package com.bw.controller;

import com.bw.domain.SysUser;
import com.bw.domain.SysUserRole;
import com.bw.service.SysUserRoleService;
import com.bw.service.SysUserService;
import com.bw.upload.FileUploadService;
import com.bw.utils.common.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author bobo棒
 * @date 2024年02月28日 15:10
 */

@RequestMapping("user")//窄化请求
@RestController
public class UserController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserRoleService sysUserRoleService;
    //@RequestBody js对象转为Java对象


    @RequestMapping("list")
    public Result list(@RequestBody SysUser sysUser,@RequestParam(defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum,3);
      List<SysUser> list=  sysUserService.list2(sysUser);
        PageInfo<SysUser> info = new PageInfo<>(list);
        return Result.success(info);
    }


/*
    @RequestMapping("list")
    public Result list(@RequestBody SysUser sysUser) {
        //构件查询对象
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.like(ObjectUtils.isNotEmpty(sysUser.getUsername()), SysUser::getUsername, sysUser.getUsername());
        qw.eq(ObjectUtils.isNotNull(sysUser.getGender()), SysUser::getGender, sysUser.getGender());
        List<SysUser> list = sysUserService.list(qw);
        return Result.success(list);
    }*/


    @RequestMapping("addOrUpdate")
    public Result add(@RequestBody SysUser sysUser){
        boolean b = sysUserService.mySave(sysUser);
        return b?Result.success():Result.fail();
    }


    @RequestMapping("delById")
    public Result delById(int id){
        boolean b = sysUserService.removeById(id);
        return b?Result.success():Result.fail();
    }

  /*  @RequestMapping("list")
    public List<SysUser> list(@RequestBody  SysUser sysUser){
         //构件查询对象
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.like(ObjectUtils.isNotEmpty(sysUser.getUsername()),"username",sysUser.getUsername());
     //   qw.like("username",sysUser.getUsername());//按名称模糊查询
        qw.eq(ObjectUtils.isNotNull(sysUser.getGender()),"gender",sysUser.getGender());

      return   sysUserService.list(qw);
    }*/


    @RequestMapping("listRoleByUserId")
    public Result listRoleByUserId(Integer userId){
       List<Integer> roleIds= sysUserRoleService.listRoleByUserId(userId);
       return Result.success(roleIds);
    }

    @Autowired
    FileUploadService FileUploadService;

    @RequestMapping("upFile")
    public Result upFile(MultipartFile file) throws IOException {
        String path = FileUploadService.uploadFile(file);
        return  Result.success(path);
/*
        String originalFilename = file.getOriginalFilename();//获取文件名
        //防止文件重名
        String newFileName= UUID.randomUUID()+originalFilename;
        //文件的上传地址
        String path="d:/pic/";
        //在本地创建一个文件
        File file1 = new File(path, newFileName);

        file.transferTo(file1);//把上传的文件写入到硬盘*/


    }
}
